package com.wgl.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgl.cmn.listener.DictListener;
import com.wgl.cmn.mapper.DictMapper;
import com.wgl.cmn.service.DictService;
import com.wgl.model.dict.Dict;
import com.wgl.model.vo.cmn.DictVo;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName DictServiceImpl.java
 * @Description TODO
 * @createTime 2022年02月04日 22:26:00
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Override
    @Cacheable(value = "dict" ,keyGenerator = "keyGenerator")
    public List<Dict> findChildData(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",id);

        return isHasChildren(baseMapper.selectList(queryWrapper));
    }

    @Override
    public void exportData(HttpServletResponse httpServletResponse) {
        //设置下载信息
        httpServletResponse.setContentType("application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        String fileName = "dict";
        httpServletResponse.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
        //查询数据库
        List<Dict> dicts = baseMapper.selectList(null);
        List<DictVo> dictVos = new ArrayList<>(dicts.size());
        for (Dict dict : dicts) {
            DictVo dictVo1 = new DictVo();
            BeanUtils.copyProperties(dict,dictVo1);
            dictVos.add(dictVo1);
        }
        try {
            EasyExcel.write(httpServletResponse.getOutputStream(), DictVo.class).sheet("dict").doWrite(dictVos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    @CacheEvict(value = "dict",allEntries = true)
    public void importData(MultipartFile multipartFile) {
        try {
            EasyExcel.read(multipartFile.getInputStream(),DictVo.class,new DictListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getDictCodeName(String dictCode, String value) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (dictCode == null){
            //直接根据value查
            queryWrapper.eq("value",value);
            return baseMapper.selectOne(queryWrapper).getName();
        } else {
            //先根据dict_code 得到记录id 再通过id查询其子记录的value=value的
            queryWrapper.eq("dict_code",dictCode);
            Dict dict = baseMapper.selectOne(queryWrapper);
            //判断是否为空
            if (dict == null){
                return  "";
            }
            //清空查询条件
            queryWrapper.clear();
            //设置查询条件
            queryWrapper.eq("value",value);
            queryWrapper.eq("parent_id",dict.getId());
            return baseMapper.selectOne(queryWrapper).getName();

        }




    }

    @Override
    public List<Dict> getdDictByDictCode(String dictCode) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_code",dictCode);
        //先获得省的id
        Dict dict = baseMapper.selectOne(queryWrapper);
        queryWrapper.clear();
        queryWrapper.eq("parent_id",dict.getId());

        return baseMapper.selectList(queryWrapper);
    }


    /**
    * @Author: Wgl
    * @Description: 判断集合数据中的数据是否还有子数据
    * @Date: 2022/2/4 22:50;
    * @param list: ;
    * @return: java.util.List<com.wgl.model.dict.Dict>;
    */
    private List<Dict>  isHasChildren(List<Dict> list){
        for (Dict dict : list) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("parent_id",dict.getId());
            dict.setHasChildren(baseMapper.selectCount(queryWrapper)>0);
        }
        return list;
    }
}
